set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [GOOGLECODERS].[BeneficiariosGet]
	@RazonSocial varchar(255) = '',
	@DirCalle varchar(255) = '',
	@DirNro int = 0,
	@DirPiso int = 0,
	@DirDepto varchar(2) = '',
	@IdRubro int = 0,
	@IdPostnet int = 0,
	@Habilitado int = 0
AS
BEGIN
	SET NOCOUNT ON;

	SELECT distinct
		b.IdBeneficiario,
		b.RazonSocial,
		b.DirCalle,
		b.DirNro,
		b.DirPiso,
		b.DirDepto,
		b.IdRubro,
		b.Habilitado,
		r.Descripcion
	FROM Beneficiarios b
	INNER JOIN Rubros r on r.IdRubro = b.IdRubro
	INNER JOIN Postnet_x_Beneficiario pb on pb.IdBeneficiario = b.IdBeneficiario
	where (@RazonSocial = '' OR b.RazonSocial LIKE '%'+@RazonSocial+'%')
	AND (@DirCalle = '' OR b.DirCalle LIKE '%'+@DirCalle+'%')
	AND (@DirNro = b.DirNro OR @DirNro = 0)
	AND (@DirPiso = b.DirPiso OR @DirPiso = 0)
	AND	(@DirDepto = b.DirDepto OR @DirDepto = '')
	AND (@Habilitado = 0 OR @Habilitado = b.Habilitado)
	AND (@IdRubro = 0 or @IdRubro = b.IdRubro)
	AND (@IdPostnet = 0 or @IdPostnet = pb.IdPostnet)
END

/*Caso de Prueba
exec [GOOGLECODERS].[BeneficiariosGet] 
	@RazonSocial = 'www',
	@DirCalle = '',
	@DirNro = 0,
	@DirPiso = 0,
	@DirDepto = '',
	@IdRubro  = 0,
	@IdPostnet  = 0,
	@Habilitado  = 1;
*/